//
//  CommentListViewController.m
//  TestARC
//
//  Created by wuxiaoquan on 13-4-8.
//
//

#import "CommentListViewController.h"
#import "SVPullToRefresh/SVPullToRefresh.h"

@interface CommentListViewController ()

@end

@implementation CommentListViewController
@synthesize commentTv,commentArray,queryId;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //[self addRoundBound:self.commentTv];
    
    if (self.commentArray == nil) {
        self.commentArray = [NSMutableArray arrayWithCapacity:10];
    }
    
    __weak CommentListViewController *weakSelf = self;
    
    // setup pull-to-refresh
    [self.commentTv addPullToRefreshWithActionHandler:^{
        [weakSelf refreshList];
    }];
    
    // setup infinite scrolling
    [self.commentTv addInfiniteScrollingWithActionHandler:^{
        //[weakSelf insertRowAtBottom];
    }];

}

-(void)viewDidUnload
{
    self.commentTv = nil;
    [super viewDidUnload];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.commentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [self.commentTv dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    
    NSDictionary *date = [self.commentArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [date objectForKey:@"comment"];
    return cell;
}

-(void)refreshList
{
    [self showWaitView:nil];
    
    NSArray  * array= [self.drugName componentsSeparatedByString:@" "];
    NSString * nameStr = [NSString stringWithFormat:@"%@  %@",[array objectAtIndex:0],[array objectAtIndex:1]];
    NSLog(@"name=%@",nameStr);
    
    //int timestamp = (int)[[NSDate date]timeIntervalSince1970];
    NSString * sort = @"0";
    NSString * pagesize = @"15";
    NSString * checksum = [[NSString stringWithFormat:@"%@%@%@%@yaoxia",self.queryId,pagesize,self.diseaseName,sort]md5];
    
    NSString *URL_DETAIL = [NSString stringWithFormat:@"http://www.yaoxia.com/ajaxServlet"];
    NSURL *url = [NSURL URLWithString:URL_DETAIL];
    request4Refresh = [ASIFormDataRequest requestWithURL:url];
    //www.yaoxia.com/ajaxServlet?style=getcommentformobile&drugid=$1&drugname=$2&diseasename=$3&currentpage=$4&pagesize=$5&sort=$6&checksum=$7
    [request4Refresh setPostValue:@"getcommentformobile" forKey:@"style"];
    [request4Refresh setPostValue:self.queryId forKey:@"drugid"];
    [request4Refresh setPostValue:nameStr forKey:@"drugname"];
    [request4Refresh setPostValue:self.diseaseName forKey:@"diseasename"];
    [request4Refresh setPostValue:@"1" forKey:@"currentpage"];
    [request4Refresh setPostValue:pagesize forKey:@"pagesize"];
    [request4Refresh setPostValue:sort forKey:@"sort"];
    //[request4Refresh setPostValue:[NSString stringWithFormat:@"%d",timestamp]  forKey:@"timestamp"];
    [request4Refresh setPostValue:checksum forKey:@"checksum"];
    //NSLog(@"style=getbaseinfo_lookformobile\ndrugid=%@\ndrugname=%@\ntimestamp=%d\nchecksum=%@",self.queryId,nameStr,timestamp,checksum);
    [request4Refresh setCompletionBlock:^{
        
        [self hideWaitView];
        [self.commentTv.pullToRefreshView stopAnimating];
        [self.commentTv.infiniteScrollingView stopAnimating];
        
        // Use when fetching text data
        NSString *responseString = [request4Refresh responseString];
        
        //NSLog(@"responseString = %@",responseString);
        id json = [responseString JSONValue];
        if ([[json objectForKey:@"code"]intValue] == 500) {
            NSLog(@"%@",[json objectForKey:@"message"]);
            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"网络加载错误"
                                  message:@"无法连接到网络"
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
            [alert show];
           
            return ;
        }
        self.commentArray = [json objectForKey:@"array"];
        //NSLog(@"diseaseArray=%@",self.diseaseArray) ;
        
        [self.commentTv beginUpdates];
        [self.commentTv reloadData];
        [self.commentTv endUpdates];
        [self.commentTv.pullToRefreshView stopAnimating];
        
        // Use when fetching binary data
        //NSData *responseData = [request responseData];
    }];
    [request4Refresh setFailedBlock:^{
        
        [self hideWaitView];
        [self.commentTv.pullToRefreshView stopAnimating];
        [self.commentTv.infiniteScrollingView stopAnimating];
        
        NSError *error = [request4Refresh error];
        
        NSLog(@"error = %@",error);
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"无法连接到网络"
                              message:nil
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }];
    [request4Refresh startAsynchronous];

}
-(void)addToListFoot
{
}

@end
